package com.wl.controller;

import com.mysql.cj.util.StringUtils;
import com.wl.pojo.Articles;
import com.wl.pojo.model.ArticleModel;
import com.wl.pojo.model.UserToken;
import com.wl.response.CommonReturnType;
import com.wl.service.ArticlesService;
import com.wl.service.impl.ArticlesServiceImpl;
import com.wl.util.MyUtil;
import com.wl.util.TokenUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-07 20:48
 */
@WebServlet("/article.action")
public class ArticleServlet extends BaseServlet {

    private ArticlesService articlesService = new ArticlesServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if (StringUtils.isNullOrEmpty(op)) {
            show404Common(request, response);
            return;
        }
        if ("browse".equals(op)) {
            browseOp(request, response);
        } else if ("insert".equals(op)) {
            insertOp(request, response);
        } else if ("delete".equals(op)) {
            deleteOp(request, response);
        } else if ("getOne".equals(op)) {
            getOneOp(request, response);
        } else if ("articleUp".equals(op)) {
            articleUpOp(request, response);
        } else if ("articleDown".equals(op)) {
            articleDownOp(request, response);
        } else if ("getArticleBySome".equals(op)) {
            getArticleBySomeOp(request, response);
        } else if ("searchArticles".equals(op)) {
            searchArticlesOp(request, response);
        } else if ("auditArticle".equals(op)) {
            auditArticleOp(request, response);
        } else if ("passAndRefusedArticle".equals(op)) {
            passAndRefusedArticleOp(request, response);
        } else if ("getArticleByLabel".equals(op)) {
            getArticleByLabelOp(request, response);
        } else if ("getAllArticle".equals(op)) {
            getAllArticleOp(request, response);
        } else if ("deleteArticle".equals(op)) {
            deleteArticleOp(request, response);
        } else if ("getOwnerArticle".equals(op)) {
            getOwnerArticleOp(request, response);
        } else if ("recommended".equals(op)) {
            recommendedOp(request, response);
        } else {
            show404Common(request, response);
        }
    }

    /**
     * 针对用户的猜他喜欢
     *
     * @param request
     * @param response
     */
    private void recommendedOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = articlesService.getUserMayBeLike(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 用户浏览文章操作
     *
     * @param request
     * @param response
     */
    private void browseOp(HttpServletRequest request, HttpServletResponse response) {
        if (!checkToken(request, response)) {
            return;
        }
        articlesService.browseArticleRecord(request);
    }

    /**
     * 获取自己所有的文章
     *
     * @param request
     * @param response
     */
    private void getOwnerArticleOp(HttpServletRequest request, HttpServletResponse response) {
        String type = request.getParameter("type");
        UserToken user = TokenUtils.getUser(request);
        String userId = user.getUserId();
        String curPage = request.getParameter("curPage");
        Map<String, Object> map = articlesService.getOwnerArticleByType(userId, type, curPage);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 管理员删除
     *
     * @param request
     * @param response
     */

    private void deleteArticleOp(HttpServletRequest request, HttpServletResponse response) {
        Articles articles = MyUtil.getTFromRequest(Articles.class, request);
        boolean b = articlesService.deleteArticleById(articles);
        if (b) {
            MyUtil.responseData(response, CommonReturnType.success(true));
        } else {
            MyUtil.responseData(response, CommonReturnType.fail("删除文章失败"));
        }
    }


    /**
     * 获取所有文章
     *
     * @param request
     * @param response
     */

    private void getAllArticleOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = articlesService.getAllArticle(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 根据标签找到相同标签的文章
     *
     * @param request
     * @param response
     */
    private void getArticleByLabelOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = articlesService.getArticleByLabel(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }


    /**
     * 通过或者不通过审核文章
     *
     * @param request
     * @param response
     */
    private void passAndRefusedArticleOp(HttpServletRequest request, HttpServletResponse response) {
        articlesService.passAndRefusedArticle(request);
        MyUtil.responseData(response, CommonReturnType.success(""));
    }

    /**
     * 获取单篇待审核的文章
     *
     * @param request
     * @param response
     */
    private void auditArticleOp(HttpServletRequest request, HttpServletResponse response) {
        Articles articles = articlesService.auditArticle(request);
        if (articles == null) {
            MyUtil.responseData(response, CommonReturnType.fail("没有待审核的文章"));
        }
        MyUtil.responseData(response, CommonReturnType.success(articles));
    }


    /**
     * 文章查询
     *
     * @param request
     * @param response
     */
    private void searchArticlesOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = articlesService.searchArticles(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 文章踩赞或者取消踩赞
     *
     * @param request
     * @param response
     */
    private void articleDownOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Long articleDownCount = articlesService.articleDown(request, request.getParameter("articleId"));
        if (articleDownCount == -1L) {
            MyUtil.responseData(response, CommonReturnType.fail("请取消点赞后再进行踩赞"));
            return;
        } else if (articleDownCount == -2L) {
            MyUtil.responseData(response, CommonReturnType.fail("此秘密已经删除"));
            return;
        }
        MyUtil.responseData(response, CommonReturnType.success(articleDownCount));
    }

    /**
     * 给文章点赞或者取消点赞
     *
     * @param request
     * @param response
     */
    private void articleUpOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Long articleUpCount = articlesService.articleUp(request, request.getParameter("articleId"));
        if (articleUpCount == -1L) {
            MyUtil.responseData(response, CommonReturnType.fail("请取消踩赞后再进行点赞"));
            return;
        } else if (articleUpCount == -2L) {
            MyUtil.responseData(response, CommonReturnType.fail("此秘密已经删除"));
            return;
        }
        MyUtil.responseData(response, CommonReturnType.success(articleUpCount));
    }

    /**
     * 根据文章Id获取单篇文章
     *
     * @param request
     * @param response
     */
    private void getOneOp(HttpServletRequest request, HttpServletResponse response) {
        String articleId = request.getParameter("articleId");
        ArticleModel articleModel = articlesService.getOneArticleById(request, articleId);
        MyUtil.responseData(response, CommonReturnType.success(articleModel));
    }

    /**
     * 删除文章操作
     *
     * @param request
     * @param response
     */
    private void deleteOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b1 = checkToken(request, response);
        if (!b1) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Articles articles = MyUtil.getTFromRequest(Articles.class, request);
        boolean b = articlesService.deleteArticleById(articles);
        if (b) {
            MyUtil.responseData(response, CommonReturnType.success(true));
        } else {
            MyUtil.responseData(response, CommonReturnType.fail("删除文章失败"));
        }
    }

    /**
     * 发表文章操作
     *
     * @param request
     * @param response
     */
    private void insertOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b1 = checkToken(request, response);
        if (!b1) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Articles articles = MyUtil.getTFromRequest(Articles.class, request);
        boolean b = articlesService.insertArticle(request, articles);
        if (b) {
            MyUtil.responseData(response, CommonReturnType.success(true));
        } else {
            MyUtil.responseData(response, CommonReturnType.fail("发表文章失败"));
        }
    }

    /**
     * 根据一些条件获取文章
     *
     * @param request
     * @param response
     */
    private void getArticleBySomeOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = articlesService.getArticleBySome(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }
}

